package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.CustomerDTO;
import com.sky.dto.CustomerLoginDTO;
import com.sky.dto.CustomerPageQueryDTO;
import com.sky.entity.Customer;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CustomerService;
import com.sky.utils.JwtUtil;
import com.sky.vo.CustomerLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/user/customer")
@Slf4j
@Api(tags = "员工登录")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param customerLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<CustomerLoginVO> login(@RequestBody CustomerLoginDTO customerLoginDTO) {
        log.info("员工登录：{}", customerLoginDTO);

        Customer customer = customerService.login(customerLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, customer.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        log.info("CustomerController jwT 创建token {}",token);
        CustomerLoginVO customerLoginVO = CustomerLoginVO.builder()
                .id(customer.getId())
                .userName(customer.getUsername())
                .name(customer.getName())
                .token(token)
                .build();

        return Result.success(customerLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     *
     * @param customerDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<String> save(@RequestBody CustomerDTO customerDTO){
        log.info("{}", customerDTO);
        customerService.save(customerDTO);
        return Result.success(customerDTO.getName()+"添加成功");
    }

    /**
     * 分页查询员工信息
     * @param customerPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工信息")
    public Result<PageResult> getCustomerList( CustomerPageQueryDTO customerPageQueryDTO){
        log.info("员工分页查询参数：{}", customerPageQueryDTO);
        PageResult pageResult= customerService.pageQuery(customerPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用，禁用员工
     * @param status
     * @param id
     * @return
     */
    @ApiOperation(value = "启用，禁用员工账号")
    @PostMapping("status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用，禁用员工账号:{},{}", status, id);
        customerService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Customer> getById(@PathVariable Long id){
        log.info("当前查询的员工id：{}",id);
        Customer customer= customerService.getById(id);
        return Result.success(customer);
    }

    /**
     * 编辑员工信息
     * @param customerDTO
     * @return
     */
    @ApiOperation("编辑员工信息")
    @PutMapping
    public Result update(@RequestBody CustomerDTO customerDTO){
        log.info("编辑员工信息：{}", customerDTO);
        customerService.update(customerDTO);
        return Result.success();
    }
}
